<?php

namespace app\modules\common\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id 主键
 * @property string $user_name 用户名
 * @property string $password 密码
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class UserModel extends \yii\db\ActiveRecord
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_time', 'update_time'], 'safe'],
            [['user_name', 'password'], 'string', 'max' => 150],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_name' => 'User Name',
            'password' => 'Password',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function getRowByUsername($username)
    {
        return (new Query())
            ->select(['id','user_name','password'])
            ->from(UserModel::tableName())
            ->where([
                'user_name' => $username
            ])->one(UserModel::getDb());
    }

    public function getRowById($userId)
    {
        return (new Query())
            ->select(['id','user_name','password'])
            ->from(UserModel::tableName())
            ->where([
                'id' => (int)$userId
            ])->one(UserModel::getDb());
    }


    public function getSessionId()
    {
        return md5(uniqid());
    }
}
